package com.letoo.sso.dataaccess.dao.cahce.impl;

import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.stereotype.Repository;

import com.letoo.sso.dataaccess.dao.cahce.CaptchaCacheDAO;

@Repository("captchaCacheDAO")
public class CaptchaCacheDAOImpl extends BaseCacheDAOImpl implements CaptchaCacheDAO {
    public static String PREFIX_REGISTER_CAPTCHA = "captcha:reg:";
    public static String PREFIX_PASSWORD_CAPTCHA = "captcha:pwd:";

    // 有效期30分钟
    public static long CAPTCHA_EXPIRE_TIME = 30 * 60;

    @Override
    public boolean setRegisterCaptcha(final String mobile, final String captcha) {
        boolean result = getRedisTemplate().execute(new RedisCallback<Boolean>() {
            @Override
            public Boolean doInRedis(RedisConnection connection) {
                byte[] key = serialize(PREFIX_REGISTER_CAPTCHA + mobile);
                String value = captcha;
                // 存储在Hash集合中
                connection.set(key, serialize(value));
                connection.expire(key, CAPTCHA_EXPIRE_TIME);

                return true;
            }
        });

        return result;
    }

    @Override
    public String getRegisterCaptcha(final String mobile) {
        String result = getRedisTemplate().execute(new RedisCallback<String>() {
            @Override
            public String doInRedis(RedisConnection connection) {
                byte[] key = serialize(PREFIX_REGISTER_CAPTCHA + mobile);
                byte[] resByte = connection.get(key);
                if (resByte == null) {
                    return null;
                }
                return deserialize(String.class, resByte);
            }
        });

        return result;
    }

    @Override
    public String getPasswordCaptcha(final String mobile) {
        String result = getRedisTemplate().execute(new RedisCallback<String>() {
            @Override
            public String doInRedis(RedisConnection connection) {
                byte[] key = serialize(PREFIX_PASSWORD_CAPTCHA + mobile);
                byte[] resByte = connection.get(key);
                if (resByte == null) {
                    return null;
                }
                return deserialize(String.class, resByte);
            }
        });

        return result;
    }

    @Override
    public boolean setPasswordCaptcha(final String mobile, final String captcha) {
        boolean result = getRedisTemplate().execute(new RedisCallback<Boolean>() {
            @Override
            public Boolean doInRedis(RedisConnection connection) {
                byte[] key = serialize(PREFIX_PASSWORD_CAPTCHA + mobile);
                String value = captcha;
                // 存储在Hash集合中
                connection.set(key, serialize(value));
                connection.expire(key, CAPTCHA_EXPIRE_TIME);

                return true;
            }
        });

        return result;
    }

    @Override
    public boolean deletePasswordCaptcha(final String mobile) {
        boolean result = getRedisTemplate().execute(new RedisCallback<Boolean>() {
            @Override
            public Boolean doInRedis(RedisConnection connection) {
                byte[] key = serialize(PREFIX_PASSWORD_CAPTCHA + mobile);
                connection.del(key);

                return true;
            }
        });

        return result;
    }

    @Override
    public Long getRegisterCaptchaTTL(final String mobile) {
        Long result = getRedisTemplate().execute(new RedisCallback<Long>() {
            @Override
            public Long doInRedis(RedisConnection connection) {
                byte[] key = serialize(PREFIX_REGISTER_CAPTCHA + mobile);
                Long resByte = connection.ttl(key);
                if (resByte == null) {
                    return null;
                }
                return resByte;
            }
        });

        return result;
    }

    @Override
    public Long getPasswordCaptchaTTL(final String mobile) {
        Long result = getRedisTemplate().execute(new RedisCallback<Long>() {
            @Override
            public Long doInRedis(RedisConnection connection) {
                byte[] key = serialize(PREFIX_PASSWORD_CAPTCHA + mobile);
                Long resByte = connection.ttl(key);
                if (resByte == null) {
                    return null;
                }
                return resByte;
            }
        });

        return result;
    }

}
